package org.example.myframework.frame.common.swagger.config;

import lombok.extern.slf4j.Slf4j;
import org.example.myframework.frame.common.swagger.bean.SwaggerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/** ━━━━━━三清保佑━━━━━━
 ** ━━━━━━佛祖保佑━━━━━━
 ** ━━━━━━上帝保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 ** ━━━━━━永无BUG━━━━━━
 ** ━━━━━━不用加班━━━━━━
 ** ━━━━━━不用出差━━━━━━
/**
 * @name: Swagger接口文档启动配置类【默认关闭】
 * @author: xyy
 * @create: 2021/5/11 15:50
 * @description:
 */

// @ConditionalOnProperty(name = "techvalley.swagger.enable", havingValue = "true")
// @EnableOpenApi
@Configuration
@Slf4j
public class SwaggerAutoConfigure {

    @Autowired
    private SwaggerProperties config;

/****************************************bean加载******************************************/


/**
     * api创建
     * @return
     */

    @Bean
    public Docket swaggerRestApi() {
        log.info("加载【api】开启swagger接口文档...............................");
        Docket docket = new Docket(DocumentationType.OAS_30)
//                .enable(config.isEnable())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(config.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                ;
        /*//判断是否加公共头部请求
        if(config.getHeaders()!=null&&config.getHeaders().length>0){
            docket.globalRequestParameters(getRequestParameters());
        }
        //判断头部是否加安全参数
        if(config.getAuthParams()!=null&&config.getAuthParams().length>0){
            docket.securityContexts(securityContexts())
                    .securitySchemes(securitySchemes());
        }*/
        return docket;
    }

/**
     * 网关聚合接口文档
     * @param oasPath
     * @return
     *//*

    @Bean
    @Primary
    @Order(Ordered.HIGHEST_PRECEDENCE + 1)
    @ConditionalOnProperty(name = "techvalley.swagger.aggDocEnable", havingValue = "true")
    public WebMvcOpenApiTransformationFilter customWebMvcOpenApiTransformer(
            @Value("${springfox.documentation.open-api.v3.path:/v3/api-docs}") String oasPath) {
        log.info("加载【api】网关聚合swagger接口文档...............................");
        return new CustomWebMvcOpenApiTransformationFilter(oasPath);
    }

    */
/****************************************信息加载******************************************//*


    */
/**
     * api信息
     * @return
     */

    private ApiInfo apiInfo() {
        ApiInfo apiInfoBuilder = new ApiInfoBuilder()
                .title(config.getTitle())
                .description(config.getDescription())
                .version(config.getVersion())
                .contact(new Contact(config.getContactName(), config.getContactUrl(), config.getEmail()))
                .build();
        return apiInfoBuilder;
    }

/**
     * 获取头部信息
     * 当全局header参数中包含命名为Accpet、Content-Type、Authorization的参数时，参数的定义将被忽略
     * @return
     *//*

    private List<RequestParameter> getRequestParameters() {
        List<RequestParameter> rpList = new ArrayList<>();
        if(config.getHeaders()!=null&&config.getHeaders().length>0){
            for(Param param : config.getHeaders() ){
                rpList.add(
                        new RequestParameterBuilder()
                                .in(ParameterType.HEADER)
                                .name(param.getName())
                                .description(param.getDesc())
                                .build()
                );
            }
        }
        return rpList;
    }

    */
/**
     * 获取安全context
     * @return
     *//*

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContextList = new ArrayList<>();
        List<SecurityReference> securityReferenceList = new ArrayList<>();
        if(config.getAuthParams()!=null&&config.getAuthParams().length>0){
            for(Param param : config.getAuthParams() ){
                securityReferenceList.add(new SecurityReference(param.getName(), scopes()));
            }
        }
        Predicate<OperationContext> selector = each -> true;
        securityContextList.add(SecurityContext
                .builder()
                .securityReferences(securityReferenceList)
                .operationSelector(selector)
                .build()
        );
        return securityContextList;
    }

    */
/**
     * 获取安全schema
     * @return
     *//*

    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        if(config.getAuthParams()!=null&&config.getAuthParams().length>0){
            for(Param param : config.getAuthParams() ){
                apiKeyList.add(new ApiKey(param.getName(), param.getDesc(), ParameterType.HEADER.getIn()));
            }
        }
        return apiKeyList;
    }

    */
/**
     * 安全范围
     * @return
     *//*

    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{new AuthorizationScope("global", "accessAnything")};
    }

*/
}

